package com.example.itas;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.HttpParams;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import com.example.classes.IP;

public class UserInfoActivity extends Activity{

	private TextView nameView;
	private TextView idView;
	private TextView cashView;
	private MyApplication myApp;
	
	private static final String TITLE = "TITLE";
	 private static final String DATE = "DATE";
	 private static final String TAG = "UserInfoActivity";
	 
	 private final String PATH = "/IServer-war/webresources/entities.userlist/getCash";
	 IP ip = new IP();
	 private final String HOST = ip.ip;
	 private final int PORT = 8080;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_userinfo);
		
		nameView = (TextView) findViewById(R.id.room_tv_name);
		idView = (TextView) findViewById(R.id.room_tv_add);
		cashView = (TextView) findViewById(R.id.room_tv_tel);
		
		myApp = (MyApplication) getApplication();
		
		idView.setText(myApp.getUid());
		
		AsyncGet ag = new AsyncGet();
		ag.execute();
	}

	private class AsyncGet extends AsyncTask<Void, Void, String> {

		protected void onPreExecute() {

			super.onPreExecute();
		}

		@Override
		protected String doInBackground(Void... params) {

			String cash = "";
			
			HttpHost target = new HttpHost(HOST, PORT);
			HttpClient client = new DefaultHttpClient();
			HttpPost post = new HttpPost(PATH);

			// set the MIME type for POST message body.
			post.setHeader("Content-Type", "application/x-www-form-urlencoded");

			HttpEntity entity = null;

			List<NameValuePair> idpair = new ArrayList<NameValuePair>();
			idpair.add(new BasicNameValuePair("ID", myApp.getUid()));
			

			try {
				
				post.setEntity(new UrlEncodedFormEntity(idpair));
				
				HttpResponse response = client.execute(target, post);

				entity = response.getEntity();
				cash = entity.toString();
				

				Log.d(TAG, "" + response.getStatusLine().getStatusCode());
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
				Log.i("errorr:", "UnsupportedEncodingException");
			} catch (ClientProtocolException e) {
				e.printStackTrace();
				Log.i("errorr:", "ClientProtocolException");
			} catch (IOException e) {
				e.printStackTrace();
				Log.i("errorr:", "IOException");
			}
			return cash;
		}

		@Override
		protected void onPostExecute(String cashResult) {
			
			cashView.setText(cashResult.toString());
			

		}
	}
	
	

}
